package com.ct.application.order.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ct.application.order.convert.OrderConvert;
import com.ct.application.order.model.po.OrderQueryPO;
import com.ct.application.order.model.vo.OrderDetailVO;
import com.ct.application.order.model.vo.OrderVO;
import com.ct.order.orm.Order;
import com.ct.order.orm.OrderDetail;
import com.ct.order.repository.admin.IAdminOrderDetailRepository;
import com.ct.order.repository.admin.IAdminOrderRepository;
import com.ct.repository.commons.PageUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.ct.order.enums.OrderStatus.*;


/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author system
 */
@Service
public class AdminOrderService {

    @Resource
    private OrderConvert orderConvert;
    @Resource
    private IAdminOrderRepository orderDao;
    @Resource
    private IAdminOrderDetailRepository orderDetailDao;


    /**
     * 订单列表
     */
    public PageUtils<OrderVO> getOrderList(OrderQueryPO po) {
        //用户id
        LambdaQueryWrapper<Order> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(!Objects.isNull(po.getMemberId()), Order::getMemberId, po.getMemberId());
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(po.getOrderNo()), Order::getOrderNo, po.getOrderNo());
        if (!Objects.isNull(po.getStatus())) {
            //查询`未付款`的订单
            if (po.getStatus() == 1) {
                lambdaQueryWrapper.eq(Order::getStatus, UNPAID.getCode());
            }
            //查询`待发货`的订单
            if (po.getStatus() == 2) {
                lambdaQueryWrapper.eq(Order::getStatus, PENDING_DELIVERY.getCode());
            }
            //查询`已发货`的订单
            if (po.getStatus() == 3) {
                lambdaQueryWrapper.eq(Order::getStatus, DELIVERED.getCode());
            }
            //查询`已完成`的订单
            if (po.getStatus() == 4) {
                lambdaQueryWrapper.eq(Order::getStatus, COMPLETED.getCode());
            }
            //查询`待评价`的订单
            if (po.getStatus() == 5) {
                lambdaQueryWrapper.eq(Order::getStatus, COMPLETED.getCode())
                        .eq(Order::getOrderAppraise, 0);
            }
        }
        lambdaQueryWrapper.orderByDesc(Order::getId);

        Page<Order> page = new Page<>(po.getPageNo(), po.getPageSize());
        IPage<Order> orderIPage = orderDao.page(page, lambdaQueryWrapper);

        List<Order> orderList = orderIPage.getRecords();
        if (orderList.isEmpty()) {
            return PageUtils.getEmptyInstance();
        }

        List<OrderVO> orderVOList = orderConvert.toOrderVOList(orderList);
        List<Long> orderIds = orderVOList.stream().map(OrderVO::getId).collect(Collectors.toList());
        List<OrderDetail> orderDetailList = orderDetailDao.getListByOrderIds(orderIds);
        List<OrderDetailVO> orderDetailVOList = orderConvert.toOrderDetailVOList(orderDetailList);
        Map<Long, List<OrderDetailVO>> orderDetailMap = orderDetailVOList.stream()
                .collect(Collectors.groupingBy(OrderDetailVO::getOrderId));
        orderVOList.forEach(orderVO -> {
            orderVO.setChildren(orderDetailMap.get(orderVO.getId()));
        });

        return new PageUtils<>(orderIPage, orderVOList);
    }


    /**
     * 订单详情
     */
    public OrderVO getOrderDetail(OrderQueryPO po) {
        LambdaQueryWrapper<Order> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(!Objects.isNull(po.getId()), Order::getId, po.getId());
        lambdaQueryWrapper.eq(!Objects.isNull(po.getMemberId()), Order::getMemberId, po.getMemberId());
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(po.getOrderNo()), Order::getOrderNo, po.getOrderNo());
        lambdaQueryWrapper.orderByDesc(Order::getCreateTime);
        if (!Objects.isNull(po.getStatus())) {
            //查询`未付款`的订单
            if (po.getStatus() == 1) {
                lambdaQueryWrapper.eq(Order::getStatus, UNPAID.getCode());
            }
            //查询`待发货`的订单
            if (po.getStatus() == 2) {
                lambdaQueryWrapper.eq(Order::getStatus, PENDING_DELIVERY.getCode());
            }
            //查询`已发货`的订单
            if (po.getStatus() == 3) {
                lambdaQueryWrapper.eq(Order::getStatus, DELIVERED.getCode());
            }
            //查询`已完成`的订单
            if (po.getStatus() == 4) {
                lambdaQueryWrapper.eq(Order::getStatus, COMPLETED.getCode());
            }
            //查询`待评价`的订单
            if (po.getStatus() == 5) {
                lambdaQueryWrapper.eq(Order::getStatus, COMPLETED.getCode())
                        .eq(Order::getOrderAppraise, 0);
            }
        }
        Order order = orderDao.getOne(lambdaQueryWrapper);
        if (order == null) {
            return null;
        }
        List<OrderDetail> orderDetailList = orderDetailDao.getListByOrderIds(Collections.singletonList(order.getId()));
        List<OrderDetailVO> orderDetailVOList = orderConvert.toOrderDetailVOList(orderDetailList);
        OrderVO orderVO = new OrderVO();
        BeanUtils.copyProperties(order, orderVO);
        orderVO.setChildren(orderDetailVOList);
        return orderVO;
    }
}

